//
// Created on 2025/1/10.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

// src/main.cpp
#include "common.h"
#include <string>
#include <vector>
#include <random>
#include <iostream>

// // 随机文本生成
// std::string generateRandomText(unsigned int seed_value) {
//     MarkovModel model(2); // 使用2-gram
//     std::string corpus_file = "D:/OpenHarmony/OpenHarmony/chinese_corpus_tokenized.txt";
//     if (!model.train(corpus_file)) {
//         std::cerr << "训练马尔可夫模型失败。" << std::endl;
//         return "";
//     }
//     std::string generated_sentence = generate_random_sentence(model, 7, seed_value);
// //     std::string generated_sentence = "你成功了!!!";
//     return generated_sentence;
// }

/*---------------------随机文本生成（之前的代码因为无法读取文件而舍弃）---------------------*/
const std::vector<std::string> chineseWords = {
    "天空", "大地", "河流", "山脉", "森林", "花朵", "鸟儿", "鱼儿", "人们", "城市",
    "阳光", "月光", "星星", "云朵", "风雨", "雷电", "春天", "夏天", "秋天", "冬天",
    "快乐", "悲伤", "愤怒", "平静", "希望", "梦想", "爱情", "友情", "亲情", "勇气"
};

std::string generateRandomText(int length) {
    std::string phrase;
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dist(0, chineseWords.size() - 1);

    for (int i = 0; i < length; ++i) {
        phrase += chineseWords[dist(gen)];
    }

    return phrase;
//     return "风雨爱情风雨鸟儿云朵";
}
/*---------------------随机文本生成（之前的代码因为无法读取文件而舍弃）---------------------*/
    
/*---------------------语音识别（文件没办法读取，所以先把结果写死）---------------------*/    
std::string whisper_recog(){
    return "风雨爱情风雨了而云朵";
}
/*---------------------语音识别（文件没办法读取，所以先把结果写死）---------------------*/  

/*---------------------文本匹配---------------------*/      
std::string match(std::string generated_sentence, std::string recognized){
    // 设置全局locale为系统默认，以支持UTF-8
    std::locale::global(std::locale("zh_CN"));
    
    std::string pinyin_map_file = "D:/OpenHarmony/OpenHarmony/pinyin_map.txt";
    if (!load_pinyin_map(pinyin_map_file)) {
        std::cerr << "加载拼音映射文件失败。" << std::endl;
        return "匹配函数出错";
    }
    /*---------------------测试中文是否乱码---------------------*/
    // return show();
    // return test();
    /*---------------------测试中文是否乱码---------------------*/
    std::string original_pinyin = text_to_pinyin(generated_sentence);
    // 识别的内容
    std::string recognized_pinyin = text_to_pinyin(recognized);
    std::cout << "识别拼音: " << recognized_pinyin << std::endl;

    // 计算相似度、编辑距离、长度差异
    double similarity = calculate_similarity(original_pinyin, recognized_pinyin);
    int edit_distance = calculate_edit_distance(original_pinyin, recognized_pinyin);
    double length_diff_ratio = 0.0;
    if (!original_pinyin.empty()) {
        length_diff_ratio = std::abs(static_cast<int>(original_pinyin.size()) -
            static_cast<int>(recognized_pinyin.size())) /
            static_cast<double>(original_pinyin.size());
    }

    std::cout << "相似度: " << similarity << std::endl;
    std::cout << "编辑距离: " << edit_distance << std::endl;
    std::cout << "长度差异比例: " << length_diff_ratio << std::endl;

    // 判断是否匹配
    bool match = is_match(generated_sentence, recognized, 0.8, 5, 0.15); // 根据需求调整阈值
    std::cout << "匹配结果: " << (match ? "匹配成功" : "匹配失败") << std::endl;

    return (match ? "匹配成功" : "匹配失败") + std::string("\n相似度:") + std::to_string(similarity) +
    "\n编辑距离: " + std::to_string(edit_distance) + "\n长度差异比例: " + std::to_string(length_diff_ratio);
}
/*---------------------文本匹配---------------------*/  
